package com.itheima.datastructure.linkedlist;

public class E02Leetcode203 {
    public  ListNode removeElements1(ListNode head,int val) {
        //哨兵节点
        ListNode s = new ListNode(-1, head);
        ListNode p1 = s;
        //ListNode p2 = s.next;
        ListNode p2=p1.next;
        while ((p2=p1.next)!= null) {
            if (p2.val == val) {
                p1.next = p2.next;
                //p2 = p2.next;
                //p2=p1.next;
            } else {
                p1 = p1.next;
                //p2 = p2.next;
                //p2=p1.next;
            }
            p2=p1.next;
        }
        return s.next;
    }

    public ListNode removeElements2(ListNode head,int val){
        if(head==null){
            return null;
        }
        if(head.val==val){
            return removeElements2(head.next,val);
        }else{
            head.next=removeElements2(head.next,val);
            return head;
        }
    }



    public static void main(String[] args) {
        ListNode head=ListNode.of(1,2,6,3,6);
        System.out.println(head);
        System.out.println(new E02Leetcode203().removeElements2(head,6));
    }
}
